package com.qf.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.qf.common.BusinessException;
import com.qf.common.ResultEntity;
import com.qf.entity.User;
import com.qf.feign.api.UserService;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/userController")
@Controller
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserPage")
    public String getUserPage(@RequestParam Map<String, Object> map, ModelMap modelMap) {
        log.debug("{}", map);
        Page<User> page = userService.getUserPage(map);
        modelMap.put("page", page);
        modelMap.addAllAttributes(map); // key:
        return "user/userList";
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public ResultEntity<Object> addUser(@RequestBody User user) {
        return ResultEntity.write(userService.addUser(user));
    }

    @RequestMapping("/getUserById/{id}")
    public String getUserById(@PathVariable Integer id, ModelMap modelMap) {
        User user = userService.getUserById(id);
        modelMap.put("user", user);
        return "user/updateUser";
    }

    @RequestMapping(value = "/updateUser")
    @ResponseBody
    public ResultEntity updateUser(@RequestBody User user) {
        return ResultEntity.write(userService.updateUser(user));
    }

    @RequestMapping(value = "/test")
    public void test(int x) {
        System.out.println(" x = "+ x);
    }


    @RequestMapping("/batchDelUser")
    @ResponseBody
    public ResultEntity batchDelUser(@RequestBody List<Integer> userIds) {
        userService.batchDelUser(userIds);
        return ResultEntity.success();
    }
}
